// 选择单程/往返
$(".trip-tab").on("click", "li", function () {
    if (!$(this).hasClass("select")) {
        $("#js-return-time").fadeToggle(100);
    }
    $(this).addClass("select")
        .siblings("li").removeClass("select");
})
$(".address-switching").on("click", "li", function () {
    $(this).addClass("select")
        .siblings("li").removeClass("select");
})

// 选择舱位弹窗显示
$("#js-space-info").click(function () {
    showMask();
    $("#js-cabin-popup").fadeIn(200);
});
selectVal("#js-cabin-popup","#js-cabin");

// 点击切换出发和到达城市
$(".icon-exchange-box").click(function () {
    var leaveVal = $("#js-leave-address").text();
    var returnVal = $("#js-return-address").text();
    $("#js-leave-address").html(returnVal);
    $("#js-return-address").html(leaveVal);
})

// 显示地址选择窗口
$(".address-box").on("click", function () {
    $("#js-address-popup").fadeIn(20);
})

// 地址选择头部搜索和取消搜索
$(".top-bar-inp").on("focus", ".input", function () {
    $("#js-btn-cancel").fadeIn(200);
    $(".top-bar-inp").animate({
        "margin-right": "132px"
    }, 200);
})
$("#js-btn-cancel").click(function () {
    $(this).fadeOut(200);
    $(this).siblings(".top-bar-inp").children(".input").val("");
    $(".top-bar-inp").animate({
        "margin-right": "38px"
    }, 200);
})

// 选中地址
$(".address-item").on("click", "a", function () {
    $("#js-address-popup").fadeOut(20);
})
$(".address-routine-item").on("click", "li", function () {
    $("#js-address-popup").fadeOut(20);
})


// 出发日期选择
$("#js-leave-time").click(function () {
    $("#ja-date-popup").removeClass("none");
    $("#ja-date-popup").find(".date-tab,.empty-box").addClass("none");
    var dateT = $(".date-top-bar").height();
    var dateH = $(window).height() - dateT;
    $(".date-box").css({"margin-top": dateT,"height": dateH});
})
// 返程日期选择
$("#js-return-time").click(function () {
    $("#js-date-leave").removeClass("select");
    $("#ja-date-popup").removeClass("none");
    $("#ja-date-popup").find(".date-tab,.empty-box").removeClass("none");
    $("#js-date-return").addClass("select");
    $("#js-date-leave").addClass("choise");
    $("#js-date-leave").append('<p class="date"><span>07月10日</span> ¥<span>890</span></p>');
    var returnWay = $(this).parents("#ja-date-popup").find(".date-tab").hasClass("none");
    if (returnWay) {
        $(".date-box").find("td").removeClass("select");
    }
    var dateT = $(".date-top-bar").height();
    var dateH = $(window).height() - dateT;
    $(".date-box").css({"margin-top": dateT,"height": dateH});
});

// 在选择返程弹框时选择出发时间
$("#js-date-leave").click(function () {
    $(this).addClass("select").siblings("li").removeClass("select");
    $(".date-box").find("td").removeClass("select");
    $(this).removeClass("choise");
    $(this).find(".date").remove();
});

$(".date-box").on("click", "td", function () {
    var overdue = $(this).hasClass("overdue");
    var tdSilings = $(".date-box").find("td");
    if (!overdue) {
        tdSilings.removeClass("select");
        $(this).addClass("select");
        var returnWay = $(this).parents("#ja-date-popup").find(".date-tab").hasClass("none");
        var selectRuren = $("#js-date-return").hasClass("select");
        console.log(returnWay);
        // 选择出发时间
        var leaveSelect = $("#js-date-leave").hasClass("select");
        var returnSelect = $("#js-date-return").hasClass("select");
        if (returnWay) { //如果选择去程
            $(this).prepend('<span class="tips">去程</span>')
            $("#ja-date-popup").addClass("none");
        } else if (!selectRuren) { //如果从返程窗口选择去程
            $("#js-date-leave").addClass("choise");
            $("#js-date-leave").append('<p class="date"><span>07月10日</span> ¥<span>890</span></p>');
            $("#js-date-leave").removeClass("select");
            $("#js-date-return").addClass("select");
        } else if (!returnWay && selectRuren) { //如果选择返程
            $("#js-date-leave").find("p.date").remove();
            $(this).prepend('<span class="tips">返程</span>');
            $("#ja-date-popup").addClass("none");
        }

    }
})

// 点击人数选择重置
$("#js-person-num").click(function () {
    posenInitialize();
    $(".person-num-box").find("li").show();
    adultScroll.goToPage(0, 1, 1000);
    childrenScroll.goToPage(0, 0, 1000);
    $(this).addClass("c-d9d9d9");
})


// 点击显示人数选择弹窗
var adultScroll,
    childrenScroll,
    adultNums,
    chidrenNums;
$("#js-porsen-num").click(function () {
    showMask();
    $("#js-person-num-fixed").slideDown(200);
    adultScroll = scrollBox('#porson-num1', '#scroller1', 1, adultCallBackFun);
    childrenScroll = scrollBox('#porson-num2', '#scroller2', 0, chidrenCallBackFun);
    adultNums = 1;
    chidrenNums = 0;
    posenInitialize();
    childrenScroll.refresh();
})

// 点击确定获取乘客人数
$("#js-person-num-determine").click(function(){
    hideMask();
    $(this).parents(".fixed").slideUp(200);
    $("#js-space-adult").html(adultNums);
    $("#js-space-children").html(chidrenNums);
});



function posenInitialize() {
    $("#scroller2").find("li").each(function () {
        var childrenMaxNum = 2;
        var value = $(this).html() + "";
        if (value && parseInt(value) > childrenMaxNum) {
            $(this).hide();
        }
    });
}

function adultCallBackFun(obj) {
    var adultNum = obj.currentPage.pageY;
    adultNums = adultNum;
    var childrenMaxNum = 0;
    switch (adultNum) {
        case 0:
            childrenMaxNum = 1;
            break;
        case 1:
            childrenMaxNum = 2;
            break;
        case 2:
            childrenMaxNum = 4;
            break;
        case 3:
            childrenMaxNum = 6;
            break;
        case 4:
            childrenMaxNum = 5;
            break;
        case 5:
            childrenMaxNum = 4;
            break;
        case 6:
            childrenMaxNum = 3;
            break;
        case 7:
            childrenMaxNum = 2;
            break;
        case 8:
            childrenMaxNum = 1;
            break;
        case 9:
            childrenMaxNum = 0;
            break;
        default:
            childrenMaxNum = 0;

    }
    $("#scroller2").find("li").each(function () {
        $(this).show();
        var value = $(this).html() + "";
        if (value && parseInt(value) > childrenMaxNum) {
            $(this).hide();
        }
    });
    childrenScroll.refresh();
    $("scroller1").attr("adultNum", adultNum);
}

function chidrenCallBackFun(obj) {
    console.log(obj.currentPage); //当前对齐捕获点   
    var childrenNum = obj.currentPage.pageY;
    chidrenNums = childrenNum;
    $("scroller2").attr("childrenNum", childrenNum);
    $("#scroller1").find("li").each(function () {
        $(this).show();
        var value = $(this).html() + "";
        if (value && parseInt(value) + childrenNum > 9) {
            $(this).hide();
        }
    });
    adultScroll.refresh();
    $("scroller2").attr("childrenNum", childrenNum);
    console.log("chidrenNums -- " +  chidrenNums);

}